package br.facet.dao;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.facet.pojo.Funcionario;
import br.facet.utils.HibernateUtils;

public class FuncionarioDAO {

	public void salvarFuncionario(Funcionario funcionario){

		Session sessao = null;
		Transaction tx = null;

		try {
			sessao = HibernateUtils.getSession();
			tx = sessao.beginTransaction();
			sessao.save(funcionario);
			tx.commit();

		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
		} finally {
			if(sessao != null){
				try {
					sessao.close();					
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}
	}

	public String obterFuncionarioPorLogin(String login){

		Session sessao = null;
		Transaction tx = null;
		String retorno = null;

		try {
			sessao = HibernateUtils.getSession();
			tx = sessao.beginTransaction();
			Query sql = sessao.createSQLQuery("Select * from tbl_funcionario where usuario = :login").setParameter("login", login);

			if(sql.uniqueResult() == null){
				retorno = "inexistente";
			}
			else{

				retorno ="existe";
			}

			tx.commit();

		} catch (Exception e) {
			tx.rollback();
			retorno = "erro";
		} finally {
			if(sessao != null){
				try {
					sessao.close();					
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}

		}
		return retorno;
	}

}
